<?php
include "Utils.php";
include "Controller.php";
include "Model.php";
include "View.php";

final class App
{
    //m:module,c:class,a:action;
    static public $siteroot;

    static public $datahost;
    static public $database;
    static public $username;
    static public $password;

    static public function init()
    {
        if ($cls = self::route("Controller")) {
            return new $cls();
        }

        header("Status: 400 Bad Request");
        exit;
    }

    static public function route($completion)
    {
        if (isset($_GET["c"])) {
            $cls = $_GET["c"];
            if (isset($_GET["m"])) {
                $mod = $_GET["m"];
                return self::import($mod, $cls, $completion);
            }
        }
        return false;
    }

    static private function import($mod, $cls, $completion)
    {
        $cls = "{$cls}{$completion}";
        $src = Path::revise(self::$siteroot . "/{$mod}/{$cls}.php");
        if (file_exists($src)) {
            include "{$src}";
            if (class_exists($cls)) {
                return $cls;
            }
        }
        return false;
    }
}